<template>
  <div>
    <van-tabs
      color="#37B5B5"
      title-inactive-color="#999999"
      title-active-color="#333333"
      v-model="active"
      @click="onClick"
    >
      <van-tab title="已上架"> </van-tab>
      <van-tab title="已下架"> </van-tab>
      <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
        <van-list
          v-model="loading"
          :finished="finished"
          :finished-text="dataOalist.length > 0 ? '没有更多了' : ''"
          @load="onLoad"
        >
          <div v-if="dataOalist.length > 0">
            <div class="toAskThe" v-for="(objContent, key) in dataOalist" :key="key">
              <div>
                <img :src="objContent.imgPath | imgPaths" />
                <div>
                  <p>{{ (!!objContent.name && objContent.name) || '' }}</p>
                  <p v-if="objContent.shareTypeCode.split(',').indexOf('SHARE_TYPE_SELL') != -1">
                    {{ (!!objContent.sellPrice && objContent.sellPrice) || '' }}/元
                  </p>
                  <p v-else>
                    {{ (!!objContent.sharePrice && objContent.sharePrice) || '' }}/{{
                      (!!objContent.unit && objContent.unit.name) || ''
                    }}
                  </p>
                  <p>{{ (!!objContent.pickUpAddress && objContent.pickUpAddress) || '' }}</p>
                </div>
              </div>
              <div>
                <p class="bjSty" v-if="active == 1" @click="editReply(objContent.id)">编辑</p>
                <p v-if="active == 1" @click="deleteContent1(objContent.id)">上架</p>
                <p v-if="active == 0" @click="deleteContent1(objContent.id)">下架</p>
                <p v-if="active == 1" @click="deleteContent(objContent.id)">删除</p>
              </div>
            </div>
          </div>
          <temporarily-data v-else />
        </van-list>
      </van-pull-refresh>
    </van-tabs>
  </div>
</template>
<script>
import {
  Form,
  Field,
  Calendar,
  RadioGroup,
  Radio,
  Dialog,
  Toast,
  DatetimePicker,
  Picker,
  Button,
  Icon,
  Cell,
  Tab,
  Tabs,
  Collapse,
  CollapseItem,
  Checkbox,
  CheckboxGroup,
  Popup,
  Image,
  PullRefresh,
  List
} from 'vant'
import dayjs from 'dayjs'
import { getValue, setValue } from '@/utils/auth'
import { postHandle, getHandle } from '@/api'
import TemporarilyData from '@/components/temporarilyData'
export default {
  name: 'JobPlacement',
  components: {
    TemporarilyData,
    [Tab.name]: Tab,
    [Tabs.name]: Tabs,
    [Cell.name]: Cell,
    [Collapse.name]: Collapse,
    [CollapseItem.name]: CollapseItem,
    [Checkbox.name]: Checkbox,
    [CheckboxGroup.name]: CheckboxGroup,
    [PullRefresh.name]: PullRefresh,
    [List.name]: List,
    [Toast.name]: Toast,
    [Dialog.Component.name]: Dialog.Component,
    [Dialog.name]: Dialog
  },
  data() {
    return {
      activeNames: [],
      dataOalist: [],
      active: 0,
      lists: [],
      result: [],
      resultSon: '',
      list: [],
      houseInfoId: getValue('fzmHourseId') || '',
      earlyWarning: [],
      totalQuantity: 0,
      loading: false,
      finished: false,
      refreshing: false,
      gxid: '',
      shieldValue:'',
      params: {
        page: 1,
        perPage: 10,
        queryDataType: 0,
        shield: 0
      }
    }
  },
  created() {},
  mounted() {
    // this.earlyWarningData(0)
    this.$bridge.register('refresh', data => {
      this.refreshing = true
      this.params.page = 1
      this.onLoad()
    })
  },
  methods: {
    // 上下架
    beforeClose1(action, done) {
      if (action === 'confirm') {
        setTimeout(() => {
          postHandle(`/agri/api/sshfw/agriculture/shield/${this.gxid}/${this.shieldValue}`).then(res => {
            this.$toast(res.msg)
            if (res.status == '0') {
              this.refreshing = true
              this.params.page = 1
              this.onLoad()
              done()
            } else {
              done()
            }
          })
        }, 1000)
      } else {
        done()
      }
    },
    // 删除
    beforeClose(action, done) {
      if (action === 'confirm') {
        setTimeout(() => {
          postHandle(`/agri/api/sshfw/agriculture/delete/${this.gxid}`).then(res => {
            this.$toast(res.msg)
            if (res.status == '0') {
              this.refreshing = true
              this.params.page = 1
              this.onLoad()
              done()
            } else {
              done()
            }
          })
        }, 1000)
      } else {
        done()
      }
    },
    deleteContent1(id) {
      this.gxid = id
      this.shieldValue = this.active == 0 ? 1 : 0
      let str = this.active == 0 ? '下架' : '上架'
      Dialog.confirm({
        title: str + '确认',
        message: '确定要'+str+'该条记录吗？',
        confirmButtonColor: 'black',
        beforeClose: this.beforeClose1
      })
    },
    deleteContent(id) {
      this.gxid = id
      Dialog.confirm({
        title: '删除确认',
        message: '确定要删除该条记录吗？',
        confirmButtonColor: 'black',
        beforeClose: this.beforeClose
      })
    },
    editReply(id) {
      setValue('gxId1', id)
      this.$bridge.call('editMyShare')
    },
    onClick(e) {
      this.dataOalist =[]
      this.refreshing = true
      this.params.page = 1
      this.params.shield = e
      this.onLoad()
    },
    onRefresh() {
      this.params.page = 1
      this.finished = false

      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      this.loading = true
      this.onLoad()
    },
    onLoad() {
      getHandle(`/agri/api/sshfw/agriculture/queryListPage`, this.params).then(res => {
        if (res.status == 0) {
          if (this.refreshing) {
            this.dataOalist = []
            this.refreshing = false
          }
          this.dataOalist = this.dataOalist.concat(res.data.rows)
          this.dataOalist.forEach(item => {
            item.imgPath = !!item.imgPath ? item.imgPath.split(',')[0] : ''
          })
          this.loading = false

          if (this.dataOalist.length >= res.data.count) {
            this.finished = true
          } else {
            this.params.page = this.params.page + 1
          }
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
::v-deep .van-tabs__wrap {
  border-bottom: 1px solid #eee;
}
.toAskThe {
  margin-top: 10px;
  background-color: #fff;
  margin-bottom: 10px;
  padding: 10px;
  > div {
    &:first-child {
      display: flex;
      border-bottom: 1px solid #eeeeee;
      padding-bottom: 10px;
      > img {
        width: 150px;
        height: 100px;
        margin-right: 15px;
      }
      > div {
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        > p {
          &:first-child {
            // min-height: 60px;
            overflow: hidden;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-line-clamp: 3;
            -webkit-box-orient: vertical;
            font-size: 14px;
            color: #333333;
            width: 100%;
          }
          &:nth-child(2) {
            font-size: 16px;
            color: #ea595d;
          }
          &:last-child {
            font-size: 12px;
            color: #999999;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
          }
        }
      }
    }
    &:last-child {
      display: flex;
      justify-content: flex-end;
      padding-top: 10px;
      > p {
        width: 50px;
        height: 24px;
        text-align: center;
        line-height: 24px;
        color: #37b5b5;
        border-radius: 2px;
        border: 1px solid #37b5b5;
        margin-left: 20px;
      }
    }
  }
}
.bjSty {
  background: #37b5b5 !important;
  color: #ffffff !important;
  border: none;
}
.van-cell__value {
  // padding: 10px 0;
  display: flex;
  align-items: center;
  > div:first-child {
    margin-right: 10px;
  }
}
.titleBox {
  margin-left: 2px;
  margin-right: 10px;
}
// .van-cell__value--alone{
//   padding: 10px 0;
// }
.titleAll {
  // position: absolute;
  // top: 20px;
  // float: right;
  padding: 12px 34px;
}
.van-cell__title {
  z-index: 10px;
}

.topCeiling {
  position: fixed;
  top: 0;
  z-index: 1;
}
.topDate {
  width: 80%;
  margin: auto;
  display: flex;
  justify-content: space-between;
  > p {
    font-size: 15px;
    &:first-child {
      width: 40%;
    }
    &:last-child {
      width: 40%;
    }
  }
}
.van-field__control {
  border: 0 !important;
}
.contents {
  //   border-bottom: 1px solid #eee;
  > .van-cell__value {
    flex-direction: column !important;
    align-items: flex-start;
  }

  padding: 10px 15px;
}
.marRight {
  margin-left: 18px;
}
.borderNone {
  border-bottom: none;
}
.col-333333 {
  color: #333333 !important;
}
.col-999999 {
  color: #999999 !important;
}
</style>
